(in-package :gwee)

(defclass drawer-view (view background-mixin title-bar-mixin)
  ((drawer-state :initform nil :accessor drawer-state)
   (full-size :initform :automatic :initarg :drawer-size :accessor drawer-full-size))
  (:documentation "A view that 'pops' open when you click on its title
and then snaps shut if you click it again."))

(defmethod mouse-press-event ((view drawer-view) button x y)
  (multiple-value-bind (tx ty tw th) (title-bar-rectangle view)
    (if (and (>= x tx)
	     (>= y ty)
	     (< x (+ tx tw))
	     (< y (+ ty th)))
	(let ((size (if (drawer-state view)
			(title-bar-size view)
			(drawer-full-size view)))
	      (offset (ecase (title-bar-side view)
			((:top :left) (if (drawer-state view)
					  (- (drawer-full-size view)
					     (title-bar-size view))
					  (- (title-bar-size view)
					     (drawer-full-size view))))
			((:bottom :right) 0))))
	  (ecase (title-bar-side view)
	    ((:top :bottom)
	     (setf (view-height view) size)
	     (incf (view-y view) offset))
	    ((:right :left)
	     (setf (view-width view) size)
	     (incf (view-x view) offset)))
	  (setf (drawer-state view) (not (drawer-state view))
		(view-needs-update-p view) t))
	(call-next-method))))
